#!/usr/bin/perl
#
# usage:
#   wget -q -O - http://localhost/cgi-bin/limura/Game/status.cgi

require 'config.ph';

$|=1;

$jsonObj = {};
@users = &GetUserNames();
$jsonObj->{"UserNames"} = \@users;
$jsonObj->{"Users"} = {};
foreach $user (@users){
	$masterURL = &GetMasterURL($user);
	$result = join('', `$WGET -q -O - $masterURL/state.cgi'?'$user`);
	$phase = 'node';
	$jsonObj->{"Users"}->{$user} = {};
	foreach $line (split(/\r?\n/, $result)){
		#USER1 line: all nodes are:
		#USER1 line: node1 alive
		#USER1 line: node10 alive
		#USER1 line: all jobs are:
		if($line =~ m/^all nodes are:/){
			$phase = 'node';
			next;
		}
		if($line =~ m/^all jobs are:/){
			$phase = 'job';
			$jobResult = '';
			next;
		}
		if($phase eq 'node'){
			($nodeName, $status, $jobs) = ($line =~ m/([^\s]+)\s+([^\s]+)\s+(.*)/);
			my(@jobIDs) = split(/\s+/, $jobs);
			$jsonObj->{"Users"}->{$user}->{"Nodes"}->{$nodeName} = {};
			$jsonObj->{"Users"}->{$user}->{"Nodes"}->{$nodeName}->{"Status"} = $status;
			$jsonObj->{"Users"}->{$user}->{"Nodes"}->{$nodeName}->{"MasterJobs"} = \@jobIDs;
			
		}
		if($phase eq 'job'){
			$jobResult .= $line;
		}
	}
	$jsonObj->{"Users"}->{$user}->{"MasterJobs"} = from_json($jobResult);
	$jsonObj->{"Users"}->{$user}->{"Point"} = &GetPoint($user);
	$jsonObj->{"Users"}->{$user}->{"MasterAddress"} = &GetUserAddress($user);
}
$jsonObj->{"GameMasterJobs"} = {};
if(opendir(DIR, "$HOME_DIR/Game/jobs")){
	while($file = readdir(DIR)){
		my($GameMasterJobResult) = "unknown";
		next if($file =~ m/^\./);
		next unless( -r "$HOME_DIR/Game/jobs/$file");
		$file =~ s/^_/UNDEFINED_/;
		my($optionString, $userName, $point, $problemType, $problem, $redundancy) = split(/_/, $file);
		$file =~ s/^UNDEFINED_/_/;
		next if($optionString eq '' || $userName eq ''
			|| $problemType eq '' || $problem eq '' || $redundancy < 0);
		if(open(FILE, "<$HOME_DIR/Game/jobs/$file")){
			my(@MasterJobs);
			while($line = <FILE>){
				$line =~ s/\r?\n$//;
				$MasterJob2GameMasterJob{$line} = $file;
				push(@MasterJobs, $line);
			}
			close(FILE);
			$jsonObj->{"GameMasterJobs"}->{$userName}->{$file}->{"MasterJobs"} = \@MasterJobs;
			$jsonObj->{"GameMasterJobs"}->{$userName}->{$file}->{"OptionString"} = $optionString;
			$jsonObj->{"GameMasterJobs"}->{$userName}->{$file}->{"ProblemType"} = $problemType;
			$jsonObj->{"GameMasterJobs"}->{$userName}->{$file}->{"Problem"} = $problem;
			$jsonObj->{"GameMasterJobs"}->{$userName}->{$file}->{"Redundancy"} = $redundancy + 0;
			$jsonObj->{"GameMasterJobs"}->{$userName}->{$file}->{"Point"} = $point + 0;
			if($GameMasterJobResult eq 'unknown'
			   && defined($jsonObj->{"Users"}->{$userName})
			   && defined($jsonObj->{"Users"}->{$userName}->{"MasterJobs"})){
				my($masterJob);
				foreach $masterJob (@MasterJobs){
					if(defined($jsonObj->{"Users"}->{$userName}->{"MasterJobs"}->{$masterJob}->{"Result"})
					   && $jsonObj->{"Users"}->{$userName}->{"MasterJobs"}->{$masterJob}->{"Result"} =~ m/^success/){
						$GameMasterJobResult = "finished";
					}
				}
			}
		}
		$jsonObj->{"GameMasterJobs"}->{$userName}->{$file}->{"Result"} = $GameMasterJobResult;
	}
	closedir(DIR);
}
foreach $user (@users){
	next if(!defined($jsonObj->{"Users"}->{$user}) || !defined($jsonObj->{"Users"}->{$user}->{"Nodes"}));
	$nodeHash = $jsonObj->{"Users"}->{$user}->{"Nodes"};
	foreach $node (keys(%$nodeHash)){
		next if(!defined($jsonObj->{"Users"}->{$user}->{"Nodes"}->{$node}->{"MasterJobs"}));
		$masterJobs = $jsonObj->{"Users"}->{$user}->{"Nodes"}->{$node}->{"MasterJobs"};
		foreach $masterJobName (@$masterJobs){
			if(defined($MasterJob2GameMasterJob{$masterJobName})){
				$jsonObj->{"Users"}->{$user}->{"Nodes"}->{$node}->{"GameMasterJob2MasterJob"}->{$MasterJob2GameMasterJob{$masterJobName}} = $masterJobName;
			}
		}
	}
}
$jsonObj->{"unixtime"} = time();
$jsonObj->{"Result"} = "success";
$jsonObj->{"Description"} = "undefined";
print to_json($jsonObj);
exit(0);

